<?php

class User extends DataObject
{
    // Declare data attributes
    protected $id;
    protected $username;
    protected $password;
    protected $available = true;
    protected $blockedreason = null;
    protected $email = null;
    protected $firstname = null;
    protected $surname = null;
    protected $picture = null;
    protected $dateofbirth = null;
    protected $interests = null;
    
    public function fetchFromDB($id, $id2=null)
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        // Name of stored procedure
        $proc = is_int($id) ? 'UserFetch' : 'UserFetchFromUsername';
        $params = array($id);
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            // Convert integer values to ints using intval();
            $this->id = is_int($id) ? intval($id) : intval($returnData[0]["id"]);
            
            $this->username = is_int($id) ? $returnData[0]["username"] : $id;
            $this->password = $returnData[0]["password"];
            
            // Boolean values return as either "1" or "0", cast to integer, then
            // to boolean
            $this->available = (bool)intval($returnData[0]["available"]);
            
            $this->blockedreason = $returnData[0]["blockedreason"];
            $this->email = $returnData[0]["email"];
            $this->firstname = $returnData[0]["firstname"];
            $this->surname = $returnData[0]["surname"];
            $this->picture = $returnData[0]["picture"];
            $this->interests = $returnData[0]["interests"];
            $this->dateofbirth = $returnData[0]["dateofbirth"];
            
            // Stored procedure succeeded, return true
            return true;
        }
        // Stored procedure failed, return false
        return false;
    }
    
    public function commit()
    {
        // Obtain the database connection
        $db = DBConnection::inst();
        
        $proc = 'UserCommit';
        $params = Array(
            $this->id,
            $this->username,
            $this->password, 
            $this->available,
            $this->blockedreason,
            $this->email,
            $this->firstname,
            $this->surname,
            $this->dateofbirth,
            $this->picture,
            $this->interests
            );
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function delete()
    {
        $db = DBConnection::inst(); 
        $proc = 'UserDelete';
        $params = array($this->id, $this->blockedreason);
        if ($db->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
    
    public function create()
    {
        if (! isset($this->id))
        {
            // No database object stored.
            $db = DBConnection::inst();
            $proc = 'UserCreate';
            $params = Array(
                $this->username,
                $this->password
            );
            
            // Sets the ID to the return value of the stored procedure, which
            // is the ID of the newly created objects.
            if ($this->id = $db->executeStoredProcedure($proc, $params, $returnData))
            {
                return true;
            }
            return false;
        }
        // data object already set
        return false;
    }
    
    public function link()
    {
        return true;
    }
    
    public function hasRoleAccess($rolename) 
    {
        if (! $this->id) return false;
        $proc = is_int($rolename) ? "UserHasRoleID" : "UserHasRoleName";
        $params = array($this->id, $rolename);
        
        if (DBConnection::inst()->executeStoredProcedure($proc, $params, $returnData))
        {
            return true;
        }
        return false;
    }
}

?>
